package com.cloudkinto.controller.user;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.vo.company.CompanyPageVo;
import com.cloudkinto.service.company.CompanyService;
import com.cloudkinto.vo.role.menu.MenuNewTreeRes;
import com.cloudkinto.service.user.UserService;
import com.cloudkinto.vo.user.UserInfoRes;
import com.cloudkinto.vo.user.login.ChangeWhReq;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张永远
 * @since 2020-05-27
 */
@RestController
@Api(tags = "wms用户模块")
@RequestMapping("/api/admin/wms/user")
public class UserWmsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(UserWmsController.class);
    @Autowired
    private UserService service;
    @Autowired
    private CompanyService companyService;

    @UserSessionRequired
    @PostMapping("/changeWh")
    @ApiOperation(value = "切换仓库", notes = "切换仓库")
    public SingleResult changeWh(@RequestBody @Validated ChangeWhReq req, HttpServletRequest request) {
        return SingleResult.success(service.changeWh(request, req, getCurrentUserId(), getTenantId()));
    }


    @UserSessionRequired
    @GetMapping("/getAllCompanyList")
    @ApiOperation(value = "获取客户列表(不受权限限制)(分页)", notes = "获取客户列表(不受权限限制)(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerQuery", value = "客户代码/客户名称"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = CompanyPageVo.class))
    public PageResult getAllCompanyList(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String customerQuery = request.getParameter("customerQuery");
        map.put("customerQuery", customerQuery);

        map.put("userId", getCurrentUserId());
        map.put("source", "wms");
        map.put("tenantId", getTenantId());
        map.put("current", current);
        map.put("pageSize", size);
        return companyService.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("getMenuTree")
    @ApiOperation(value = "获取菜单列表", notes = "获取菜单列表")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = MenuNewTreeRes.class))
    public SingleResult getMenuTree(@RequestHeader String token) {
        return SingleResult.success("", service.getMenuTree(token, "wms"));
    }

    @UserSessionRequired
    @GetMapping("getUserInfo")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = UserInfoRes.class))
    public SingleResult getUserInfo(@RequestHeader String token) {
        return SingleResult.success("", service.getUserInfo(token, "wms"));
    }
}

